<?php
/**
 * Created by Maxotm.
 * User: tuyt
 * Date: 2019/5/17
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Requests\UserRequest;
use App\Handlers\ImageUploadHandler;
use Illuminate\Support\Facades\Storage;

class UsersController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth', ['except' => ['show']]);
    }

    public function show(User $user)
    {
        return view('users.show', compact('user'));
    }
    public function edit(User $user)
    {
        return view('users.edit', compact('user'));
    }

    public function update(UserRequest $request, User $user,ImageUploadHandler $uploader)
    {
        $data = $request->all();

        if ($request->avatar) {
            if ($user->avatar) {
                Storage::disk('admin')->delete($user->avatar);
            }
            $result = $uploader->save($request->avatar,'avatar',$user->id,416);
            if ($result) {
                $data['avatar'] = $result['path'];
            }
        }
        $user->update($data);
        return redirect()->route('users.show', $user->id)->with('success', '个人资料更新成功！');
    }

}